package com.gxa.controller;

import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.param.MyTableParam;
import com.gxa.pojo.Empl;
import com.gxa.pojo.Role;
import com.gxa.service.roleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author xiejinfeng
 * @create 2021-06-10 13:32
 * @descrip:
 */
@Controller
@RequestMapping("/role")
public class roleController {

    @Autowired
    private roleService service;

    @PostMapping("/list/data")
    @ResponseBody
    public LayR list(){
        return service.findAll();
    }

    @PostMapping("/list/Pagedata")
    @ResponseBody
    public LayR list1(MyTableParam param){
        return service.list(param);
    }

    @GetMapping("/list/page")
    public String listPage(){
        return "role/role-list";
    }

    @GetMapping("/add/page")
    public String addPage(){
        return "role/role-add";
    }

    @PostMapping("/add/do")
    @ResponseBody
    public R add(Role role){
        return service.add(role);
    }

    @PostMapping("/deleteOne")
    @ResponseBody
    public R deleteOne(Integer roleId){

        return service.deleteOne(roleId);

    }

    @PostMapping("/delete")
    @ResponseBody
    public R delete(String[] roleIds){
        return service.deleteAll(roleIds);
    }

    @GetMapping("/edit/page")
    public String editPage(Integer id, Model model, HttpSession session){
        session.setAttribute("editRoleId",id);
        model.addAttribute("editRoleId", id);
        return "role/role-edit";
    }

    @PostMapping("/detail")
    @ResponseBody
    public R detail(Integer id){
        return service.findById(id);
    }

    @PostMapping("/edit/do")
    @ResponseBody
    public R edit(Role role, HttpSession session){
        Integer editRoleId = (Integer) session.getAttribute("editRoleId");
        role.setRoleId(editRoleId);
        return service.edit(role);
    }
}
